package cn.cupk.corporateshuttlemanagesystem.controller;

import cn.cupk.corporateshuttlemanagesystem.mapper.UserMapper;
import cn.cupk.corporateshuttlemanagesystem.mapper.CompanyReservationInformationMapper;
import cn.cupk.corporateshuttlemanagesystem.pojo.Routes;
import cn.cupk.corporateshuttlemanagesystem.pojo.Users;
import cn.cupk.corporateshuttlemanagesystem.result.PageResult;
import cn.cupk.corporateshuttlemanagesystem.result.RequestParams;
import cn.cupk.corporateshuttlemanagesystem.result.Result;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 公司预约信息管理
 */
@RestController
@RequestMapping("/ydxxs")
@Slf4j
public class CompanyReservationInformationController {
    @Autowired
    private CompanyReservationInformationMapper companyReservationInformationMapper;

    @Autowired
    private UserMapper userMapper;

    @RequestMapping("/page")
    public Result<Object> page(@RequestBody RequestParams<Routes> params) {
        // 获取用户信息
        String userId = params.getData().getUserId();
        Users userById = userMapper.findUserById(Integer.parseInt(userId));
        // 判断用户是否存在
        if (userById != null)
            params.getData().setCompanyId(userById.getCompanyId());
        // 分页查询
        Page<Object> page = PageHelper.startPage(params.getPageNum(), params.getPageSize());
        List<Routes> routes = companyReservationInformationMapper.page(params.getData());
        // 遍历查询结果
        List<Routes> routes1 = new ArrayList<>();
        for (Routes route : routes) {
            String stopsId = route.getStopsId();
            // 判断 stopsId 是否为空
            String[] stationName = stopsId.split("，");
            List<Integer> stationId = new ArrayList<>();
            // 遍历 stationName
            for (String s : stationName) {
                // 将 s 转换为 Integer 类型
                stationId.add(Integer.parseInt(s.trim()));
            }
            List<String> list = companyReservationInformationMapper.stopList(stationId);
            route.setStationName(list);
//
            String[] split = route.getUserId().split("，");
            // 判断 split 中是否包含 params.getData().getUserId()
            if (split.length > 0) {
                for (String s : split) {
                    // 将 s 转换为 Integer 类型
                    int i = Integer.parseInt(s);
                    Users userById1 = userMapper.findUserById(i);
                    if (userById1 != null) {
                        // 创建 Routes 对象
                        Routes routes2 = new Routes();
                        // 复制属性
                        BeanUtils.copyProperties(route, routes2);
                        routes2.setUserName(userById1.getName());
                        routes1.add(routes2);
                    }
                }
            }
        }
        return Result.ok(new PageResult<>(page.getTotal(), routes1));
    }

}